package com.yonyou.brigade.third_parties.third.service.impl;

import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.model.CtripBigUser;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.model.CtripPostDictionaries;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.ICtripBigUserService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.service.ICtripPostDictionariesService;
import com.yonyou.brigade.third_parties.ctrip.bigcustomer.util.EmployeeSyncUtils;
import com.yonyou.brigade.third_parties.ctrip.model.CtripConfig;
import com.yonyou.brigade.third_parties.third.model.BriDeptAuthorization;
import com.yonyou.brigade.third_parties.third.model.BriThirdProperty;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserver;
import com.yonyou.brigade.third_parties.third.service.IBriDeptAuthorizationService;
import com.yonyou.brigade.third_parties.third.service.IBriThirdPropertyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CtripAuthUserUpdateObserver  implements IAuthUserUpdateObserver {

    private static Logger logger = LoggerFactory.getLogger(CtripAuthUserUpdateObserver.class);

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("ctripBigUserServiceImpl")
    private ICtripBigUserService ctripBCUserService;

    @Autowired
    @Qualifier("briThirdPropertyServiceImpl")
    private IBriThirdPropertyService thirdPropertyService;

    @Autowired
    @Qualifier("briDeptAuthorizationServiceImpl")
    private IBriDeptAuthorizationService briDeptAuthorizationService;

    @Autowired
    @Qualifier("ctripPostDictionariesServiceImpl")
    private ICtripPostDictionariesService iCtripPostDictionariesService;



    public Map updateAuthUser(List<String> userids, String orgpk, String authUserid)
    {
        Map result=new HashMap();
        try {
            updateCtripAuthUser(userids,orgpk,authUserid);
            result.put("code","0");
            result.put("information","操作成功");
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
            result.put("code","1");
            result.put("information","更新携程用户信息操作失败");
        }
        return result;
    }

    private void updateCtripAuthUser(List<String> userids, String orgpk,String authUserid)
    {
        String tenant="";
        CtripConfig config=new CtripConfig();
        config=(CtripConfig)briChannelService.getChannelConfig(tenant,ChannelEnum.CTRIPBC.getName(),config);
        if(config!=null) {
            if (userids != null && userids.size() > 0) {
                List<String> tempUserids = new ArrayList<>(userids);
                tempUserids.add(authUserid);
                    //检查当前要更新的用户是不是有还没有同步的人员，如果没有则进行同步
                    List<CtripBigUser> ctripUsers = ctripBCUserService.findNeedAsynUsersByListAuth(tempUserids);
                    if (ctripUsers != null && ctripUsers.size() > 0) {
                        asynCtripUsers(ctripUsers, config);
                    }
                    //更新用户的信息，包括授权人等
                    List<CtripBigUser> updateCtripUsers = ctripBCUserService.findCtripUserListForUpdate(userids);
                    List<BriDeptAuthorization> deptAuths = briDeptAuthorizationService.selectDeptAuthInfoByOrg(orgpk);
                    Map params=new HashMap();
                    params.put("channel",ChannelEnum.CTRIPBC);
                    List<BriThirdProperty> thirdProperties=thirdPropertyService.findPropsByChannel(params);
                    //yhz携程职务
                    if (updateCtripUsers!=null && updateCtripUsers.size()>0){
                        for (CtripBigUser ctripUser : updateCtripUsers){
//                            if (!StringUtil.isEmpty(ctripUser.getPostname())){
                            CtripPostDictionaries ctripPostDictionaries = iCtripPostDictionariesService.getPostDictionaries(ctripUser.getPostName());
                            if (ctripPostDictionaries!=null){
                                ctripUser.setIsBookClass(ctripPostDictionaries.getIsBookClass());
                                ctripUser.setIntlBookClassBlock(ctripPostDictionaries.getIntlBookClassBlock());
                                if(StringUtils.isEmpty(ctripUser.getPostName()))
                                {
                                    ctripUser.setPostName(ctripPostDictionaries.getPostName());
                                }
                            }
//                            }
                        }
                    }
                    EmployeeSyncUtils.multipleEmployeeWithAuthSync(updateCtripUsers, config, deptAuths, true,thirdProperties);
            }
        }
    }

    private void asynCtripUsers(List<CtripBigUser> ctripUsers, CtripConfig config)
    {
        Map params =new HashMap();
        params.put("channel",ChannelEnum.CTRIPBC.getName());
        List<BriThirdProperty> thirdProperties=thirdPropertyService.findPropsByChannel(params);
        EmployeeSyncUtils.multipleEmployeeWithAuthSync(ctripUsers, config,null,false,thirdProperties);
        ctripBCUserService.insertCtripUserBatch(ctripUsers);
    }

//    private boolean isNC()
//    {
//        NCInfo ncInfo = ncInfoService.selectByUserid(null);
//        if(ncInfo.getOstype().equals("nc"))
//        {
//            return true;
//        }
//        else
//        {
//            return  false;
//        }
//    }
}
